package com.throttle.raildroid;

import java.util.ArrayList;
import java.util.Date;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;
import javax.swing.event.EventListenerList;

public class RailDroidActivity extends TabActivity {
    /** Called when the activity is first created. */
    
    GestureDetector	gestures;
    View tab1Layout;
    TabHost tabHost;
    
    public class refresher implements Runnable{
    //this class actively refreshed the tabs at 30 frames a second. There was no way to actively refresh the buttons in the 
    //switch map.
		public void run() {
			Date curDate = new Date();
			Date newDate = new Date();
			while(true){
				newDate = new Date();
				if (newDate.getTime() - curDate.getTime() >34){
					tabHost.postInvalidate();
				}
			}
		}
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
        setContentView(R.layout.main);

        tabHost=getTabHost();
        refresher r = new refresher();
        new Thread(r).start();
        // no need to call TabHost.Setup()        
        
        //First Tab
        TabSpec spec1=tabHost.newTabSpec("Init");
        spec1.setIndicator("Init");
        Intent in1=new Intent(this, InitActivity.class);
        spec1.setContent(in1);
        
        TabSpec spec2=tabHost.newTabSpec("Switches");
        spec2.setIndicator("Switches");
        Intent in2=new Intent(this,RunActivity.class);
        spec2.setContent(in2);
        
        
        TabSpec spec3=tabHost.newTabSpec("Train 1");
        spec3.setIndicator("Train 1");
        Intent in3=new Intent(this,HelpActivity.class);
        spec3.setContent(in3);
        
        TabSpec spec4=tabHost.newTabSpec("Train 2");
        spec4.setIndicator("Train 2");
        Intent in4=new Intent(this,HelpActivity.class);
        spec4.setContent(in4);
        
        TabSpec spec5=tabHost.newTabSpec("Train 3");
        spec5.setIndicator("Train 3");
        Intent in5=new Intent(this,HelpActivity.class);
        spec5.setContent(in5);
        
        TabSpec spec6=tabHost.newTabSpec("Log");
        spec6.setIndicator("Log");
        Intent in6=new Intent(this,SelectActivity.class);
        spec6.setContent(in6);
        
        TabSpec spec7=tabHost.newTabSpec("Train 4");
        spec7.setIndicator("Train 4");
        Intent in7=new Intent(this,HelpActivity.class);
        spec7.setContent(in7);
        
        StaticLogList.testHandler.addRailEventListener(new RailEventListenerInf() {
			public void RailEventOccurred(RailEvent evt) {
				StaticLogList.logList.add(evt.msg);
			}
		});
        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);
        tabHost.addTab(spec4);
        tabHost.addTab(spec5);
        tabHost.addTab(spec7);
        tabHost.addTab(spec6);
        
        
        StaticLogList.testHandler.fireMyEvent(new RailEvent(true,"Tab bar made."));
        
        tabHost.setCurrentTab(5);
        tabHost.setCurrentTab(0);
        
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            public void onTabChanged(String tabId) {
            	
		    }
        });

        
        
        int tabCount = getTabWidget().getChildCount();
        for (int i = 0; i< tabCount; i++)
        {
        	final int tabNum = i;
        	getTabWidget().getChildAt(i).setOnClickListener(new OnClickListener() {
        		
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				tabHost.getCurrentView().setAnimation(outToLeftAnimation());
    				tabHost.setCurrentTab(tabNum);
    				tabHost.getCurrentView().setAnimation(inFromRightAnimation());
    			} 
			});
		}
    }
        
    
    
    public Animation inFromRightAnimation() {

        Animation inFromRight = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, +1.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f
        );
        inFromRight.setDuration(250);
        inFromRight.setInterpolator(new AccelerateInterpolator());
        return inFromRight;
    }
    
    public Animation outToLeftAnimation() {
        Animation outtoLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        outtoLeft.setDuration(250);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
    }
    
    public Animation inFromLeftAnimation() {
        Animation outtoLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        outtoLeft.setDuration(250);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
    }
    
    public Animation outToRightAnimation() {
        Animation outtoLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, +1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        outtoLeft.setDuration(250);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
    }
 
}